package com.boy.controller.admin;


import com.boy.entity.Permission;
import com.boy.service.PermissionService;
import com.boy.tool.annotate.RequirePermissions;
import com.boy.tool.domain.ResultVo;
import com.boy.tool.annotate.WhdLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author whd
 * @since 2024-05-23
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @WhdLog
    @RequirePermissions({"Permission:create"})
    @PostMapping("/create")
    public ResultVo create(@RequestBody Permission permission) {
        if (permissionService.save(permission)) return ResultVo.success(permission);
        return ResultVo.failure();
    }

    @RequirePermissions({"Permission:find"})
    @PostMapping("/find")
    public ResultVo find() {
        return ResultVo.success(permissionService.findAll());
    }

    @WhdLog
    @RequirePermissions({"Permission:update"})
    @PostMapping("/update")
    public ResultVo update(@RequestBody Permission permission) {
        if (permissionService.updateById(permission))
            return ResultVo.success(permission);
        return ResultVo.failure();
    }


    @RequirePermissions({"Permission:get"})
    @PostMapping("/get")
    public ResultVo get(@RequestParam String id) {
        Permission permission = permissionService.getById(id);
        if (permission == null) return ResultVo.failure();
        return ResultVo.success(permission);
    }

    @WhdLog
    @RequirePermissions({"Permission:delete"})
    @PostMapping("/delete")
    public ResultVo delete(@RequestParam String id) {
        if (permissionService.removeById(id)) return ResultVo.success("删除成功");
        return ResultVo.failure();
    }
}

